package com.xdtech.play.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.play.model.PlayMember;
import com.xdtech.play.service.PlayMemberService;
import com.xdtech.play.vo.PlayMemberItem;
import com.xdtech.play.vo.PlayerItem;
import com.xdtech.web.controller.BaseController;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2016-05-08 23:42:50
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class PlayMemberController extends BaseController{
	@Autowired
	private PlayMemberService playMemberService;
	@RequestMapping(value="/playMember.do",params = "playMember")
	public ModelAndView skipPlayMember() {
		return new ModelAndView("play/playMember/playMember_ftl");
	}
	
	@RequestMapping(value="/playMember.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(PlayMemberItem item,Pagination pg) {
		Map<String, Object> results =  playMemberService.loadPageDataByConditions(pg,item,"findPlayMemberByCondition");
		return results;
	}
	@RequestMapping(params = "checkName")
	@ResponseBody
	public boolean checkName(String name,String memberId) {
		if(StringUtils.isBlank(memberId)) memberId = "0";
		boolean rs = playMemberService.checkNameIsExist(Long.valueOf(memberId),name);
		return !rs;
	}
	
	@RequestMapping(value="/playMember.do",params = "skip")
	public ModelAndView skip(HttpServletRequest request,String page,Long memberId) {
		if (memberId!=null) {
			request.setAttribute("operationDate", DateUtil.getDate(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			request.setAttribute("playMemberItem", playMemberService.loadPlayMemberItem(memberId));
		}
		return new ModelAndView("play/playMember/"+page+"_ftl");
	}
	
	
	
	@RequestMapping(value="/playMember.do",params = "editPlayMember")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editPlayMember(HttpServletRequest request,Long playMemberId) {
		log.info("test");
		PlayMemberItem playMemberItem = null;
		if (playMemberId!=null) {
			playMemberItem = playMemberService.loadPlayMemberItem(playMemberId);
		}else {
			playMemberItem = new PlayMemberItem();
			playMemberItem.setMoney(0);
		}
		request.setAttribute("playMemberItem", playMemberItem);
		return new ModelAndView("play/playMember/editPlayMember_ftl");
	}
	
	@RequestMapping(value="/playMember.do",params = "savePlayMember")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage savePlayMember(PlayMemberItem item) {
		ResultMessage r = new ResultMessage();
		if (playMemberService.saveOrUpdatePlayMember(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/playMember.do",params = "loadCombobox")
	@ResponseBody
	public List<PlayerItem> loadCombobox(PlayMemberItem item) {
		ResultMessage r = new ResultMessage();
		List<PlayMember> members = playMemberService.getAll();
		return PlayerItem.createItems(members);
	}
	
	@RequestMapping(value="/playMember.do",params = "deletePlayMemberItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deletePlayMemberItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> playMemberIds = new ArrayList<Long>();
			for (String id : tempIds) {
				playMemberIds.add(Long.valueOf(id));
			}
			playMemberService.deletePlayMemberInfo(playMemberIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
